<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      unique-opened
      router
      @select="handleSelect"
    >
      <template v-for="item in items">
        <template v-if="item.children">
          <el-submenu :index="item.path" :key="item.path">
            <template slot="title">
              <i :class="item.meta.icon"></i>
              <span slot="title">{{ item.meta.title }}</span>
            </template>
            <template v-for="subItem in item.children">
              <el-submenu
                v-if="subItem.children"
                :index="subItem.path"
                :key="subItem.path"
              >
                <template slot="title">{{ subItem.meta.title }}</template>
                <template v-for="threeItem in subItem.children">
                  <el-submenu
                    v-if="threeItem.children"
                    :index="threeItem.path"
                    :key="threeItem.path"
                  >
                    <template slot="title">{{ threeItem.meta.title }}</template>
                  </el-submenu>
                  <el-menu-item
                    v-else-if="threeItem.meta.type !== 'INNER_MENU'"
                    :index="threeItem.path"
                    :key="threeItem.path"
                  >
                    {{ threeItem.meta.title }}
                  </el-menu-item>
                </template>
              </el-submenu>
              <el-menu-item
                v-else-if="subItem.meta.type !== 'INNER_MENU'"
                :index="subItem.path"
                :key="subItem.path"
              >
                {{ subItem.meta.title }}
              </el-menu-item>
            </template>
          </el-submenu>
        </template>
        <template v-else-if="item.meta.title !== '登录'">
          <el-menu-item :index="item.path" :key="item.path">
            <i :class="item.meta.icon"></i>
            <span slot="title">{{ item.meta.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from "./bus";
import store from "@/store/";
export default {
  data() {
    return {
      items: store.getters.sidebar[0].children.concat(
        this.$router.options.routes
      )
    };
  },
  inject: ["reload"], // 注入重载的功能（注入依赖）
  methods: {
    handleSelect() {
      this.reload(); // 点击侧边栏重新载入页面
    }
  },
  computed: {
    onRoutes() {
      return this.$route.path.replace("/", "");
    }
  },
  created() {
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on("collapse", msg => {
      this.collapse = msg;
      bus.$emit("collapse-content", msg);
    });
  }
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 180px;
}
.sidebar > ul {
  height: 100%;
}
</style>
